require.config({
    paths: {
        'jquery': ['http://cdn.bootcss.com/jquery/2.2.4/jquery.min', '/js/lib/jquery.min'],
        'doT': '/js/lib/doT.min',
        'select2': '/js/lib/select2.min',
        'bootstrap': '/js/lib/bootstrap.min',
        'paginator': '/js/lib/bootstrap-paginator'
    },
    shim: {
        'bootstrap': ['jquery'],
        'paginator': ['bootstrap']
    }
});

var myFun = {};

require(['jquery', 'doT', 'select2', 'common', 'bootstrap', 'paginator'], function($, doT, select2, common) {
    $(function() {
        $(document).ajaxStart(function() {
            $('#tips').show();
        }).ajaxStop(function() {
            $('#tips').hide();
        });

        var pageSize = 10;
        var role = sessionStorage.getItem('role');
        var school_id = sessionStorage.getItem('school_id');

        $('#school').on('change', function() {
            refreshCanteen(1);
        });

        function initSchool() {
            var all = { 'id': 'all', 'name': '全部' };
            $.get(common.urlBase + "/school/allSchool.json", function(data, statu) {
                data = eval('(' + data + ')');
                var schools = data.data;
                myFun.schoolList = data.data;
                schools.unshift(all);
                var schoolTmpl = doT.template($('#template_school').text());
                $('#school').html(schoolTmpl(schools));
                $('#school').select2();
                if (role != 1) {
                    $('#school').attr('disabled', true);
                    $('#school').val(school_id);
                    $('#select2-school-container').text($('#school option[value='+school_id +']').text());
                }

                refreshCanteen(1);
            });
        }
        initSchool();

        function getCanteen(page, pageSize) {
            var d = { 'page': page, 'rows': pageSize };
            var canteensList = new Array();
            var school_id = $('#school').val();
            var canteenTmpl = doT.template($('#template_canteen').text());

            if (school_id == 'all') {
                $.post(common.urlBase + '/shop/desktop/getAllShop/getAllShop.json', d, function(data, statu) {
                    var data = eval('(' + data + ')');
                    if (data['ok']) {
                        $('#table_canteen').show();
                        canteensList = data.data.obj;
                        $.each(canteensList, function(index, value) {
                            $.each(myFun.schoolList, function() {
                                if (canteensList[index].school_id == this.id) {
                                    canteensList[index].school_name = this.name;
                                }
                            });
                        });
                        $('#canteen').html(canteenTmpl(canteensList));

                        var total = data.data.count;
                        $('#total').text(total);
                        var pageNumber = Math.ceil(total / pageSize);
                        initPaginator(page, 8, pageNumber);
                        $('#paginator_canteen').show();
                    } else {
                        $('#table_canteen').hide();
                        $('#noticeMsg').text(data['message']);
                        $('#noticeModal').modal('show');
                        return false;
                    }
                });
            } else {
                $.get(common.urlBase + '/shop/desktop/school-shop/' + school_id + '.json', function(data, statu) {
                    $('#paginator_canteen').hide();
                    $('#canteen').html('');
                    var data = eval('(' + data + ')');
                    if (data['ok']) {
                        $('#table_canteen').show();
                        canteensList = data.data;
                        $.each(canteensList, function(index, value) {
                            $.each(myFun.schoolList, function() {
                                if (canteensList[index].school_id == this.id) {
                                    canteensList[index].school_name = this.name;
                                }
                            });
                        });
                        $('#canteen').html(canteenTmpl(canteensList));
                    } else {
                        $('#table_canteen').hide();
                        $('#noticeMsg').text(data['message']);
                        $('#noticeModal').modal('show');
                        return false;
                    }
                });
            }
        }

        function refreshCanteen(page) {
            getCanteen(page, pageSize);
        }


        //初始化分页组件
        function initPaginator(currentPage, numberOfPages, totalPages) {
            var options = {
                currentPage: currentPage,
                numberOfPages: numberOfPages,
                totalPages: totalPages,
                itemTexts: function(type, page, current) {
                    switch (type) {
                        case 'first':
                            return '首页';
                        case 'prev':
                            return '上一页';
                        case 'next':
                            return '下一页';
                        case 'last':
                            return '末页';
                        case 'page':
                            return page;

                    }
                },
                onPageChanged: function(event, oldPage, newPage) {
                    refreshCanteen(newPage);
                }
            };
            $('#paginator').bootstrapPaginator(options);
        }


        myFun.deleteCanteen = function deleteAdmin(obj) {
            var id = $(obj).data('target'); //获得按钮对应的条目信息
            if (confirm('是否确认删除？')) {
                $.get('http://tin168.com/packer/shop/deleteShop/' + id + '.json', function(data, statu) {
                    data = eval('(' + data + ')')
                    if (data['ok']) {
                        location.reload();
                    } else {
                        $('#noticeMsg').text(data['message']);
                        $('#noticeModal').modal('show');
                    }
                });
            }
        }
        myFun.changeCanteen = function changeCanteen(obj) {
            var id = $(obj).data('target');
            $(obj).attr('href', '/views/change_canteen.html?id=' + id);
        }

    });

});
